package com.odata.springboot.starter.odata.odata.sql.process.impl;

import com.odata.springboot.starter.odata.odata.sql.filterMethod.ParseFilterProcess;
import com.odata.springboot.starter.odata.odata.sql.process.SqlProcess;
import com.odata.springboot.starter.odata.odata.sql.process.SqlProcessChain;
import com.odata.springboot.starter.odata.odata.sql.sqlBuild.OdataSqlInformation;
import org.apache.olingo.server.api.OData;
import org.apache.olingo.server.api.uri.UriInfo;
import org.apache.olingo.server.api.uri.queryoption.FilterOption;
import org.apache.olingo.server.api.uri.queryoption.expression.Expression;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * filter 的处理
 * @author chenYX
 * @date 2021/6/28 11:16
 * @description
 */
@Component
public class FilterProcessImpl extends SqlProcess {

    @Autowired
    private SqlProcessChain sqlProcessChain;

    @Autowired
    private ParseFilterProcess parseFilterProcess;

    @Override
    public void process(OdataSqlInformation.SqlInformationBuild sqlBuild, UriInfo uriInfo, Class<?> aClass, OData oData) {
        FilterOption filterOption = uriInfo.getFilterOption();
        if (filterOption != null) {
            Expression expression = filterOption.getExpression();
            sqlBuild.createFilterString();
            sqlBuild.buildFilterFlag(true);
            //交给 parseFilterProcess 处理
            parseFilterProcess.processFilterExpression(expression,sqlBuild);
        }else {
            sqlBuild.buildFilterFlag(false);
        }
    }


    @Override
    public void afterPropertiesSet() throws Exception {
        this.sqlProcessChain.addSqlProcess(this);
    }




}
